#!/bin/sh

TIME_SYNC_COMPLETE="no"
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
    if /usr/bin/timedatectl status | grep -q "synchronized: yes"; then
        TIME_SYNC_COMPLETE="yes"
        break
    else
        echo "timedatectl: System time not yet synchronized; try #${i}" >&2
        sleep 5
    fi
done

if [ "${TIME_SYNC_COMPLETE}" = "no" ]; then
    echo "Timeout waiting for time synchronization to complete." >&2
else
    echo "System time now properly synchronized; try #${i}" >&2
fi

echo "Exiting $0 at $(date)" >&2
exit 0
